package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class AddGoodsReource extends BaseRepair {

    public AddGoodsReource(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(long serverId,Map<Integer, String> beautyResourceMap,Map<Integer, String> skinResourceMap) {
        Map<Long, List<Integer>> beautyMap = getUserBeauty();
        for (Map.Entry<Long, List<Integer>> entry : beautyMap.entrySet()) {
            Long userId = entry.getKey();
            for (Integer beautyId : entry.getValue()) {
                if (beautyId == 103313) {
                    String resource = beautyResourceMap.get(beautyId);
                    saveResource(userId,beautyId,resource);
                }
            }
        }

        Map<Long, List<Integer>> skinMap = getUserSkin();
        for (Map.Entry<Long, List<Integer>> entry : skinMap.entrySet()) {
            Long userId = entry.getKey();
            for (Integer skinId : entry.getValue()) {
                if (skinId == 109507) {
                    String resource = skinResourceMap.get(skinId);
                    saveResource(userId,skinId,resource);
                }
            }
        }

    }

    private void saveResource(long userId,int goodsId,String resource) {
        getLogger().info("userId {}, beautyId {}, resource: {}",userId,goodsId,resource);
        PreparedStatement call = null;
        String sql = "REPLACE INTO t_u_goods_resource (userId,goodsId,resourceFlag) values (?,?,?)";
        try {
            call = getGameServer().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, goodsId);
            call.setString(i++, resource);
            boolean b = call.executeUpdate() > 0;
        } catch (Exception e) {
        } finally {
            getGameServer().closeConnection(call, null);
        }
    }


    public Map<Long,List<Integer>> getUserBeauty() {
        Map<Long,List<Integer>> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,beautyId from t_u_beauty  ";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                int beautyId = rs.getInt("beautyId");
                if (!dataMap.containsKey(userId)) {
                    dataMap.put(userId, new ArrayList<>());
                }
                dataMap.get(userId).add(beautyId);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Long,List<Integer>> getUserSkin() {
        Map<Long,List<Integer>> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,skinId from t_u_skin ";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                int beautyId = rs.getInt("skinId");
                if (!dataMap.containsKey(userId)) {
                    dataMap.put(userId, new ArrayList<>());
                }
                dataMap.get(userId).add(beautyId);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataMap;
    }


}
